
$(function(){
	$("#selectAll").on("click",function(){
		if($(this).attr("checked")){
			$("#contentTable tbody #tdCheckbox").find("input[type='checkbox']").attr("checked",true);
		}else{
			$("#contentTable tbody #tdCheckbox").find("input[type='checkbox']").attr("checked",false);
		}
	});
	
	 $('#btnReset').click(function(){
			$('.ipt-search,.Wdate').val('');
			$('.select2-container').select2("val", '');
	 });
	 
});	



//阻止enter键提交表单
function form_keypress(e){
	if(e.which==13||e.keyCode==13){
		return false;
	}
}

/**
 * 浏览器分辨率判断
 */
function screen_(){
	var res=false;
	if(screen.height>=1024){
		res=true;
	}
	return res;
}

/**
 * 获取浏览器类型
 */
function getBrowser(){
	var res="";
	var Sys = {};
    var ua = navigator.userAgent.toLowerCase();
    var s;
    (s = ua.match(/rv:([\d.]+)\) like gecko/)) ? Sys.ie = s[1] :
    (s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1] :
    (s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] :
    (s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] :
    (s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] :
    (s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0;
    
    if (Sys.ie)res='IE ' + Sys.ie;
    if (Sys.firefox)res='Firefox ' + Sys.firefox;
    if (Sys.chrome)res='Chrome ' + Sys.chrome;
    if (Sys.opera)res='Opera ' + Sys.opera;
    if (Sys.safari)res='Safari ' + Sys.safari;
    return res;
}

function isIE8(){
    return navigator.userAgent.split(";")[1].toLowerCase().indexOf("msie 8.0")=="-1"?false:true;
}
    
// 引入js和css文件
function include(id, path, file){
	if (document.getElementById(id)==null){
        var files = typeof file == "string" ? [file] : file;
        for (var i = 0; i < files.length; i++){
            var name = files[i].replace(/^\s|\s$/g, "");
            var att = name.split('.');
            var ext = att[att.length - 1].toLowerCase();
            var isCSS = ext == "css";
            var tag = isCSS ? "link" : "script";
            var attr = isCSS ? " type='text/css' rel='stylesheet' " : " type='text/javascript' ";
            var link = (isCSS ? "href" : "src") + "='" + path + name + "'";
            document.write("<" + tag + (i==0?" id="+id:"") + attr + link + "></" + tag + ">");
        }
	}
}

// 获取URL地址参数
function getQueryString(name, url) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    if (!url || url == ""){
	    url = window.location.search;
    }else{	
    	url = url.substring(url.indexOf("?"));
    }
    r = url.substr(1).match(reg)
    if (r != null) return unescape(r[2]); return null;
}

//获取字典标签
function getDictLabel(data, value, defaultValue){
	for (var i=0; i<data.length; i++){
		var row = data[i];
		if (row.value == value){
			return row.label;
		}
	}
	return defaultValue;
}

// 打开一个窗体
function windowOpen(url, name, width, height){
	var top=parseInt((window.screen.height-height)/2,10),left=parseInt((window.screen.width-width)/2,10),
		options="location=no,menubar=no,toolbar=no,dependent=yes,minimizable=no,modal=yes,alwaysRaised=yes,"+
		"resizable=yes,scrollbars=yes,"+"width="+width+",height="+height+",top="+top+",left="+left;
	window.open(url ,name , options);
}

/*内部弹窗，方便回调*/
function openWindowInner(url, title, width, height) {
	title = title||'未命名';
	width = (width||800) + "px";
	height = (height||400) + "px";
	var index = layer.open({
				title : title,
				type : 2,
				skin : 'layer-ext-espresso',
				content : url,
				shade : [0.2, '#666666'],
				area : [width, height],
				shadeClose : true
			});
	return index;
}

function loading(mess){
	var id='loader'+Math.random();
	if (mess == undefined || mess == ""){
		mess = "正在提交，请稍等...";
	}
	layer.msg(mess,{icon:16,id:id});
}


// 警告对话框
function alertx(data, closed){
	 if(data.code==0){//导入失败
		 layer.msg(data.message);
		 setTimeout(alertFunc, 3000);
	 }else if(data.code==1){//导入成功
		 layer.msg(data.message);
		 setTimeout(alertFunc, 1000);
	 }
}
function alertFunc() {
	closeWindow();
}
//确认对话框
function confirmx(mess,obj,href){
	obj = obj||{title:'提示',icon: 7};
	top.layer.confirm(mess, obj, function(index){	
		if (typeof href == 'function') {
			href();
		}else{
			location = href;
		}
		top.layer.close(index);
	});
	return false;	
}

// 提示输入对话框
/*onclick="return promptx('删除流程','删除原因',this.href);"*/
/*function promptx(title, lable, href, closed){
	$.jBox("<div class='form-search' style='padding:20px;text-align:center;'>" + lable + "：<input type='text' id='txt' name='txt'/></div>", {
			title: title, submit: function (v, h, f){
	    if (f.txt == '') {
	        $.jBox.tip("请输入" + lable + "。", 'error');
	        return false;
	    }
		if (typeof href == 'function') {
			href();
		}else{
			resetTip(); //loading();
			location = href + encodeURIComponent(f.txt);
		}
	},closed:function(){
		if (typeof closed == 'function') {
			closed();
		}
	}});
	return false;
}*/



//使用layer作为弹窗层
function openWindow(url, title, width, height) {
	title = title||'未命名';
	width = (width||800) + "px";
	height = (height||400) + "px";
	var index = top.layer.open({
				title : title,
				type : 2,
				skin : 'layer-ext-espresso',
				content : url,
				shade : [0.2, '#666666'],
				area : [width, height],
				shadeClose : true
			});
	return index;
}

/*内部弹窗，方便回调*/
function openWindowInner(url, title, width, height) {
	title = title||'未命名';
	width = (width||800) + "px";
	height = (height||400) + "px";
	var index = layer.open({
				title : title,
				type : 2,
				skin : 'layer-ext-espresso',
				content : url,
				shade : [0.2, '#666666'],
				area : [width, height],
				shadeClose : true
			});
	return index;
}



//关闭layer
function closeWindow(index) {
	index = index || parent.layer.getFrameIndex(window.name);
	parent.layer.close(index);
}


//easyui右下角
function _alertShow(msg){
	if(parent.$.messager){
		parent.$.messager.show({
	    	title:'系统提示',
	    	msg:msg,
	    	timeout:3000,
	    	showType:'slide'
	        });		
		
	}else{
		$.messager.show({
	    	title:'系统提示',
	    	msg:msg,
	    	timeout:3000,
	    	showType:'slide'
	        });		
		
	}

}	


//添加TAB页面
function addTabPage(title, url, closeable, $this, refresh){
	debugger;
	top.$.fn.jerichoTab.addTab({
        tabFirer: $this,
        title: title,
        closeable: closeable == undefined,
        data: {
            dataType: 'iframe',
            dataLink: url
        }
    }).loadData(refresh != undefined);
}



// cookie操作
function cookie(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + options.path : '';
        var domain = options.domain ? '; domain=' + options.domain : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
}

// 数值前补零
function pad(num, n) {
    var len = num.toString().length;
    while(len < n) {
        num = "0" + num;
        len++;
    }
    return num;
}

// 转换为日期
function strToDate(date){
	return new Date(date.replace(/-/g,"/"));
}

//获取当前日期
function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;
    return currentdate;
}

// 日期加减
function addDate(date, dadd){  
	date = date.valueOf();
	date = date + dadd * 24 * 60 * 60 * 1000;
	return new Date(date);  
}

//截取字符串，区别汉字和英文
function abbr(name, maxLength){  
 if(!maxLength){  
     maxLength = 20;  
 }  
 if(name==null||name.length<1){  
     return "";  
 }  
 var w = 0;//字符串长度，一个汉字长度为2   
 var s = 0;//汉字个数   
 var p = false;//判断字符串当前循环的前一个字符是否为汉字   
 var b = false;//判断字符串当前循环的字符是否为汉字   
 var nameSub;  
 for (var i=0; i<name.length; i++) {  
    if(i>1 && b==false){  
         p = false;  
    }  
    if(i>1 && b==true){  
         p = true;  
    }  
    var c = name.charCodeAt(i);  
    //单字节加1   
    if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) {  
         w++;  
         b = false;  
    }else {  
         w+=2;  
         s++;  
         b = true;  
    }  
    if(w>maxLength && i<=name.length-1){  
         if(b==true && p==true){  
             nameSub = name.substring(0,i-2)+"...";  
         }  
         if(b==false && p==false){  
             nameSub = name.substring(0,i-3)+"...";  
         }  
         if(b==true && p==false){  
             nameSub = name.substring(0,i-2)+"...";  
         }  
         if(p==true){  
             nameSub = name.substring(0,i-2)+"...";  
         }  
         break;  
    }  
 }  
 if(w<=maxLength){  
     return name;  
 }  
 return nameSub;  
}

function myAutoComplete(id,min,url){
	var minLen=min==undefined||min==null?2:min;
	var cache = {};
	$(id).autocomplete({
		delay: 500,
		source:function(request,response){
			var term = request.term;
            if ( term in cache ) {
                response( $.map( cache[ term ], function( item ) {
                    return {
                        value: item.enterpriseName
                    }
                }));
                return;
            }
		 	$.ajax({
				url:ctx +url,
				dataType:'json',
				type:'POST',
				data:{
					enterpriseName:term,searchType:1
				},
				success:function(data){
					 cache[term] = data;
					 response( $.map( data, function( item ) {
                        return {
                            value: item.enterpriseName
                        }
                    }));
				}
			});
		},
		minLength:minLen,
		select:function(event,ui){
			return ui.item.enterpriseName;
		}
	});
}

/**
 * 表单数据装载
 * @param jsonStr
 */
function loadFormData(jsonStr){
    var obj = jsonStr;
    var key,value,tagName,type,arr;
    for(x in obj){
        key = x;
        value = obj[x];
         
        $("[name='"+key+"'],[name='"+key+"[]']").each(function(){
            tagName = $(this)[0].tagName;
            type = $(this).attr('type');
            readonly=$(this).attr('readonly');
            if(tagName=='INPUT'){
                if(type=='radio'){
                    $(this).attr('checked',$(this).val()==value);
                }else if(type=='checkbox'){
                    arr = value.split(',');
                    for(var i =0;i<arr.length;i++){
                        if($(this).val()==arr[i]){
                            $(this).attr('checked',true);
                            break;
                        }
                    }
                }else if(type=='text'){
                    $(this).val(value);
                }
            }else if(tagName=='SELECT'){
            	if(readonly==undefined)$(this).select2('val',value);
            }else if( tagName=='TEXTAREA'){
            	 $(this).text(value);
            }
             
        });
    }
}

function compileStr(code){
    var c=String.fromCharCode(code.charCodeAt(0)+code.length);  
    for(var i=1;i<code.length;i++)  
     {        
      c+=String.fromCharCode(code.charCodeAt(i)+code.charCodeAt(i-1));  
     }   
    return c;   
}

/*把时间格式化为日期字符串显示,jgrid列表*/
function formatterDateTimeToDate(value,o,row){
    if(value){
	return value.substring(0,10);
    }else{
    return "";
    }
}

